<template>
	<view class="tabbar">
		<view class="tab-item" :class="{active: current === 0}" @click="switchTab(0)">
			<text>{{titles[0]}}</text>
		</view>
		<view class="tab-item" :class="{active: current === 1}" @click="switchTab(1)">
			<text>{{titles[1]}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			current: {
				type: Number,
				default: 0
			},
			titles: {
				type: Array,
				default: () => ['首页', '我的']
			},
			pagePaths: {
				type: Array,
				default: () => [
					'/pages/index/index',
					'/pages/mine/mine',
				]
			},
			needLogin: {
				type: Array,
				default: () => [false, true]
			}
		},
		methods: {
			switchTab(index) {
				// 检查是否需要登录
				if (this.needLogin[index]) {
					const token = uni.getStorageSync('token')
					if (!token) {
						uni.showModal({
							title: '提示',
							content: '请先登录',
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
							}
						})
						return
					}
				}

				// 如果点击当前页则不跳转
				if (index === this.current) return

				// 跳转到对应页面
				uni.switchTab({
					url: this.pagePaths[index]
				})
			}
		}
	}
</script>

<style>
	.tabbar {
		height: 120rpx;
		border-top: 2rpx solid #eee;
		background-color: #fff;
		display: flex;
		justify-content: space-around;
		align-items: center;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 999;
	}

	.tab-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.tab-item image {
		width: 48rpx;
		height: 48rpx;
	}

	.tab-item text {
		margin-top: 8rpx;
		font-size: 24rpx;
	}

	.tab-item.active text {
		color: #007aff;
	}
</style>